from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.contrib.auth import authenticate
from django.core.context_processors import auth
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.http import HttpResponseRedirect



def login_authenticate(request):
	username = request.POST.get('username', '')
	password = request.POST.get('password', '') 
	user = authenticate(username=username, password=password)
	if user is not None and user.is_active:
		auth.login(request, user)
		return HttpResponse("Logged In")
	else:
		return HttpResponse("not authenticated")

def login_form(request):
	return render_to_response('login.html')



def create_user(request):
	if request.method == 'POST':
		  form = UserCreationForm(request.POST)
		  if form.is_valid():

		  	   new_user = form.save()
		  	   return HttpResponse("You're registered")
		  else:
		  	   form = UserCreationForm()
		  	   return render_to_response("create_user.html", {'form': form})
	else:
		form = UserCreationForm()
		return render_to_response("create_user.html", {'form': form})

def display_meta(request):
	values = request.META.items()
	values.sort()
	return render_to_response('request_params.html', locals())   	
       	    

def hello(request):	
	if request.user.is_authenticated():
		return HttpResponse("autheticated")
	else:
		return HttpResponse("not authenticated")
	
def display_meta(request):
	values = request.META.items()
	values.sort()
	return render_to_response('request_params.html', locals())
	
def current_url_view_good(request):
	return HttpResponse("Welcome to the page at %s" % request.path)
	
def css_test(request):
	return render_to_response('css_test.html')

def object_list(request, model):
	obj_list = model.objects.all()
	template_name = 'mysite/%s_list.html' % model.__name__.lower()
	return render_to_response(template_name, {'object_list': obj_list})
	
